package cn.reinforce.util.aliyun;

import cn.reinforce.util.aliyun.exception.AliyunException;
import cn.reinforce.util.aliyun.model.ErrorResponse;
import cn.reinforce.util.aliyun.model.market.ContentCheck;
import cn.reinforce.util.aliyun.model.market.ContentCheckResponse;
import cn.reinforce.util.aliyun.model.ocr.OcrRequest;
import cn.reinforce.util.aliyun.model.ocr.OcrResponse;
import cn.reinforce.util.http.HttpClientUtil;
import cn.reinforce.util.http.model.HttpResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阿里云市场的一些接口
 * @author 幻幻Fate
 * @create 2020/9/22
 * @since
 */
public class AliyunMarketUtil {

    public static ContentCheck checkContent(String in, String appCode) {
        String url = "http://monitoring.market.alicloudapi.com/neirongjiance";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appCode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        Gson gson = new GsonBuilder().create();

        Map<String, Object> data = new HashMap<>();
        data.put("in", in);

        HttpResult result = HttpClientUtil.post(url, data, headers, null, false);

        ContentCheckResponse contentCheckResponse = gson.fromJson(result.getResult(), ContentCheckResponse.class);

//        if(ocrResponse == null){
//            ErrorResponse errorResponse = gson.fromJson(result.getResult(), ErrorResponse.class);
//            throw new AliyunException(errorResponse.getErrorMsg());
//        }

        ContentCheck contentCheck = contentCheckResponse.getData().getOut().get(0);

        boolean flag = false;

        flag = contentCheck.get政治敏感监测().size() > 0 || contentCheck.get违禁品监测().size() > 0 || contentCheck.get恶意灌水监测().get(0) || contentCheck.get色情监测().size() > 0 || contentCheck.get辱骂监测().size() > 0;

        contentCheck.setFlag(flag);

        return contentCheck;
    }
}
